package be.djdb.manipulators.streams;

import java.io.*;
import java.lang.reflect.Array;

import java.util.logging.Level;
import java.util.logging.Logger;

import be.djdb.Statics;
import be.djdb.manipulators.streams.util.Stringcontentrecorder;
import be.djdb.manipulators.streams.util.StringtagFinder;


/**
* @author Lieven Roegiers
* @copyright 2007
* @from UpdateCV
*/
@Deprecated
public class StreamManipulator implements StreamManipulators,Serializable{
	private static final long serialVersionUID = -5201498374375383550L;
	private static final boolean DEBUG = false;
	private Object tagname;
	private char delemiter;
	/**
	 * @param input
	 * @param find
	 * @param replacer
	 * @return
	 * @throws IOException
	 *  * <test url(IMG:"value")>      he take the IMG: and replaced in wat you want StreampieceStringreplacer(String replacement) 
	 * findAndReplace(inputstream,"IMG:",StreamPieceUtilConverter replacer) throws IOException
	 */
	public void findAndReplace(InputStream input,String tagname,StreamPieceUtilConverter replacer,OutputStreamWriter Doutstream) throws IOException{
		StringtagFinder finder = new StringtagFinder(tagname);
		try{
			log(Level.WARNING,"TOFINDand replace:"+ tagname +"ACTION:"+replacer.getClass().getName());
		}catch(NullPointerException e){
			log(Level.WARNING, replacer.getClass().getName()+"IS NOT A GOOD ACCEPT FUNCTION");
		}
		boolean fullrecstarted = false;//rec opnemen van value in buffer 
		boolean isbuffer= false;//is buffer in gebruik
		int charint;
		if (DEBUG){System.out.print("<<<<<<<<<<<<<<<<<DEBUG=>start>>>>>>>>>>>");}
	        while ((charint = input.read()) != -1 ){
	         	if(finder.ismatchfound()){
	        		isbuffer = true;
	        		    	try {
								replacer.replace(finder.getbuffer(),Doutstream);
							} catch (Exception e) {
								e.printStackTrace();
							}
					finder.SetReset();
	          			
				}else if(isbuffer){//niet positive match geconsumeerde chars writen
					//Doutstream.append("<print buffer /> >>");
					if (DEBUG){System.out.print(finder.getbuffer());}
					if (DEBUG){System.out.print((char)charint);}
					Doutstream.append(finder.getbuffer());//schrijven v/d char s dat mogelijk tag was
					Doutstream.append((char)charint);//laaste actief char naar buffer schrijven.
					isbuffer = false ;
					finder.SetReset();
				}else{
	        		/*Niets aan de hand gewoon doorpompen van de stream
	        		 **/
	        		if (DEBUG){Doutstream.append(">>");}
	        		Doutstream.append((char)charint);
				}
	        }
	        //Doutstream.flush();
	        if (DEBUG){System.out.print("<<<<<<<<<<<<<<<<<DEBUG=>DONE>>>>>>>>>>>");}
	        //return new ByteArrayInputStream(out.toString().getBytes());//ow.toString();// out.t;
		}

	/** to find IMG:"path"
	 * @param input
	 * @param tag =>"IMG:"
	 * @param delemiter =>'"'
	 * @throws IOException
	 */
	public Array[] splitter(char tag){
		return null;
	}
	public String getStreamPiece(){
		return null;
	}
	private char[]trimarray(char[]array,int length){
		char buffer[] = new char[length];
		System.arraycopy(array, 0, buffer, 0, length);//arraycopy(Object srcarray, int srcPos,Object destarray,int destPos,int length)
		return buffer;
	}
	/**
	 * @param input
	 * @param tagname
	 * @param delemiter
	 * @param replacer
	 * @return
	 * @throws IOException
	 * <test url(IMG:"value")>       than can he take the value
	 * findAndReplace(inputstream,"IMG:",'"',new imageincluder()) throws IOException
	 */
	/*public void findAndReplace(InputStream input,String tagname,char delemiter,StreamPieceUtilConverter replacer,DataOutputStream ost) throws IOException {
		String lemiter = null ;
		lemiter += delemiter;	
		//return 
		findAndReplace(input, tagname,lemiter, replacer,ost);
    }*/
	public void findAndReplace(InputStream input,String tagname,String delemiter,StreamPieceUtilConverter replacer,OutputStreamWriter Doutstream) throws IOException{
	StringtagFinder finder = new StringtagFinder(tagname);
	Stringcontentrecorder recorder = new Stringcontentrecorder(delemiter.charAt(0));
	try{
		log(Level.WARNING,"TOFIND:"+ tagname + delemiter+"GET_THIS_VALUE"+delemiter+"ACTION:"+replacer.getClass().getName());
	}catch(NullPointerException e){
		log(Level.WARNING, replacer.getClass().getName()+"IS NOT A GOOD ACCEPT FUNCTION");
	}
	boolean fullrecstarted = false;//rec opnemen van value in buffer 
	boolean isbuffer= false;//is buffer in gebruik
	int charint;
	if (DEBUG){log(Level.WARNING,"<<<<<<<<<<<<<<<<<DEBUG=>start>>>>>>>>>>>");}
        while ((charint = input.read()) != -1 ){	
         	if(finder.ismatchfound()||recorder.getenabled()||recorder.ismatchfound()|| finder.tagfinder(charint)){
        		isbuffer = true;
        		/*als value ook gekend is**/
        		if (recorder.ismatchfound()){//Start actie
        			try {
						replacer.replace(recorder.getvalue(),Doutstream);
					} catch (Exception e) {
						e.printStackTrace();
					}
        			Doutstream.append((char)charint);//laaste actief char naar buffer schrijven.
        			recorder.SetReset();
        		}
        		else{
        			/*is de delemiter gevonden of was hij al gevonden*/
         			if(recorder.contentfinder(charint)||recorder.getenabled()){
         				finder.SetReset();
         				if (DEBUG){Doutstream.append("<rec>"+(char)charint+"</rec>");}
         			}
             	}        			
			}else if(isbuffer){//niet positive match geconsumeerde chars writen
				Doutstream.append(finder.getbuffer());//schrijven v/d char s dat mogelijk tag was
				Doutstream.append((char)charint);//laaste actief char naar buffer schrijven.
				isbuffer = false ;
				finder.SetReset();
			}else{
        		/*Niets aan de hand gewoon doorpompen van de stream
        		 **/
        		Doutstream.append((char)charint);
			}
        }
        if (DEBUG){log(Level.WARNING,"<<<<<<<<<<<<<<<<<DEBUG=>DONE>>>>>>>>>>>");}
	}
    private static  void log(Level level, String msg ){
        String tag ="<>>>>*"+Statics.LIBNAME+"-"+Statics.COPYRIGHTBY+"*<<<<>StreamManipulator";
        Logger.getLogger(StreamManipulator.class.getName()).log(level, tag + msg);
    }
}

